package publish

import (
	"context"
	"encoding/json"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"

	"group-card/api/internal/svc"
	"group-card/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PublishListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPublishListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PublishListLogic {
	return &PublishListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PublishListLogic) PublishList(req *types.ListPublishRequest) (resp *types.ListPublishResponse, err error) {
	data, err := l.svcCtx.Card.PublishList(l.ctx, &cardclient.PublishListRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("获取卡片发行列表失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.ListPublishResponse{Code: response.ErrCode, Message: "获取卡片发行列表失败"}, nil
	}

	return &types.ListPublishResponse{
		Code:     response.SuccessCode,
		Message:  "获取卡片发行列表成功",
		PageNum:  req.PageNum,
		Data:     data.List,
		PageSize: req.PageSize,
		Success:  true,
		Total:    data.Total,
	}, nil
}
